package com.vote.controllers;

import java.net.URLDecoder;

import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.vote.model.User;
import com.vote.services.GroupService;
import com.vote.utils.HostHelper;

import net.paoding.rose.web.ControllerInterceptorAdapter;
import net.paoding.rose.web.Invocation;

public class StaticInterceptor extends ControllerInterceptorAdapter {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(StaticInterceptor.class);
	
	private static final Logger accesslogger = Logger.getLogger("vote.access");
	
	@Autowired
	private GroupService groupService;

	@Override
    public Object before(Invocation inv) throws Exception {
		User host = HostHelper.getHost(inv.getRequest());
		
		accesslogger.info((host == null ? 0 : host.getUserId()) + "|" + inv.getRequest().getRequestURI());
		
        inv.addModel("HostUser", host);
		inv.addModel("HomeUrl", "http://"  +  inv.getRequest().getServerName()  +  ":"  +  inv.getRequest().getServerPort()  +  inv.getRequest().getContextPath());
        return true;
    }

    @Override
    public Object after(Invocation inv, Object instruction) throws Exception {
        inv.addModel("qGroupList", groupService.getGroupList());
        return instruction;
    }
}
